<style lang="scss" scoped>
@import "~@/assets/styles/mixin.scss";
@import "~@/assets/styles/variables.scss";

.app-main {
  min-height: calc(100vh - 84px);
}

.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, .08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
    line-height: 60px;
    padding-left: 40px;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .search-menu {
    line-height: 60px !important;
    margin-right: 20px;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 18px;
          font-size: 12px;
        }
      }
    }
  }
}

.dept-main-container {
  height: 100%;
  overflow: hidden;
  position: relative;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
}

.main-containe-header {
  max-height: 84px;
  border-bottom: 1px solid #d8dce5;
}
.app-container{
  //min-height: 300px;
  height: 90vh;
}
</style>
<template>
  <div
    class="app-wrapper"
  >
    <div class="dept-main-container">
      <div class="main-containe-header">
        <div class="navbar">
          <div class="hamburger-container"></div>
          <div class="breadcrumb-container">设置密码</div>
          <div class="right-menu">
            <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
              <div class="avatar-wrapper">
                你好, 欢迎使用
                <i class="el-icon-caret-bottom"/>
              </div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item divided @click.native="logout">
                  <span>退出</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>

        </div>
      </div>
      <div class="app-main">

        <div class="app-container">

          <div class="login">
            <el-form ref="subForm" :model="subForm" :rules="formRules" class="login-form">
              <h3 class="title">修改密码</h3>

              <el-form-item prop="passwd">
                <el-input
                  v-model="subForm.passwd"
                  type="password"
                  auto-complete="off"
                  placeholder="密码"
                >
                  <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
                </el-input>
              </el-form-item>
              <el-form-item prop="passwd">
                <el-input
                  v-model="subForm.confirmPasswd"
                  type="password"
                  auto-complete="off"
                  placeholder="确认密码"
                >
                  <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
                </el-input>
              </el-form-item>
              <el-form-item style="width:100%;">
                <el-button
                  :loading="loading"
                  size="medium"
                  type="primary"
                  style="width:100%;"
                  @click.native.prevent="submit"
                >
                  <span v-if="!loading">提交</span>
                  <span v-else>提 交 中...</span>
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { setPwd } from "@/api/authority_app/base";
import Cookies from "js-cookie";
import { getRandomSecretKey } from '@/utils/index'
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {

    let validatePass = (rule,value,callback) => {

      if(value === '') {

        callback(new Error('新密码不能为空'));
      } else if(value.length < 6) {

        callback(new Error('新密码不能小于6位'));
      } else if(value.length > 20) {

        callback(new Error('新密码不能大于20位'));
      } else {

        callback();
      }
    };
    let validateConfirmPass = (rule,value,callback) => {

      if(value === '') {

        callback(new Error('确认新密码不能为空'));
      } else if(value.length < 6) {

        callback(new Error('确认密码不能小于6位'));
      } else if(value.length > 20) {

        callback(new Error('确认密码不能大于20位'));
      } else if(value.length > 20) {

        callback(new Error('确认密码与新密码不一致'));
      } else {

        callback();
      }
    };

    return {
      subForm: {
        passwd: "",
        confirmPasswd: '',
      },
      formRules: {
        passwd: [
          {
            trigger: "blur",
            validator: validatePass
          }
        ],
        confirmPasswd: [
          {
            trigger: "blur",
            validator: validateConfirmPass
          }
        ],
      },
      loading: false
    };
  },
  methods: {

    submit() {
      this.$refs.subForm.validate(valid => {
        if (valid) {

          let secretKey = getRandomSecretKey()
          this.loading = true;
          this.subForm.passwd = encrypt(secretKey,this.subForm.passwd)
          this.subForm.paconfirmPasswdsswd = encrypt(secretKey,this.subForm.confirmPasswd)
          setPwd({
            'key': secretKey,
            'newPassword': this.subForm.passwd,
            'confirmNewPassword': this.subForm.confirmPasswd
          }).then(() => {
            this.loading = false;

            this.$store.dispatch("GetInfo").then((res) => {

              this.$router.replace({ path: "/" });
            })
          }).catch(res=> {
            this.loading = false;
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
</style>













